package com.lzy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzy.annotation.DataScope;
import com.lzy.dao.SysUserPostDao;
import com.lzy.entity.SysUserPostEntity;
import com.lzy.service.ISysPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.lzy.dao.SysPostDao;
import com.lzy.entity.SysPostEntity;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;


@Service("sysPostService")
public class SysPostService extends ServiceImpl<SysPostDao, SysPostEntity> implements ISysPostService {

    @Autowired
    private SysPostDao mapper;

    @Autowired
    private SysUserPostDao sysUserPostDao;

    @Override
    @DataScope
    public IPage<Map<String, Object>> selectPageList(SysPostEntity sysPostEntity) {
      Page<Map<String, Object>> result = new Page<>(sysPostEntity.getPage().getPageNum(), sysPostEntity.getPage().getPageSize());
      return mapper.selectPageList(result, sysPostEntity);
    }

    @Override
    public List<SysPostEntity> getCurUserPostInfo(Long userId) {
      List<SysUserPostEntity> sysUserPostEntities = sysUserPostDao.selectList(new QueryWrapper<SysUserPostEntity>().eq("user_id", userId));
      Set<Long> collect = sysUserPostEntities.stream().map(item -> item.getPostId()).collect(Collectors.toSet());

      return baseMapper.selectList(new QueryWrapper<SysPostEntity>().in("post_id", collect));
    }
}